package com.mzx.wechat321.utill;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mzx.wechat321.pojo.ReplyMessage;
import com.mzx.wechat321.pojo.UserRqPojo;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;

import static com.mzx.wechat321.utill.ByteToUtill.byteToStr;

/**
 * @author Mzx
 */
public class MyUtill {

    // 与接口配置信息中的Token要一致
    private static String token = "mzxtoken";

    // 查询电量工具
    public static String getCont(String search)  throws IOException {
        System.out.println(search);
        JSONObject jsonObject = JSON.parseObject(JsoupUtill.toGet(search));
        String ov = "宿舍号:"+jsonObject.get("description")+"\n剩余电量:"+jsonObject.get("quantity")+"度";
        return ov;
    }

    // 封装用户请求
    public static ReplyMessage getReplyMessage(HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        Map<String, String> map = WechatMessageUtil.xmlToMap(request);
        replyMessage.setMsgType(map.get("MsgType"));
        replyMessage.setFromUserName(map.get("ToUserName"));
        replyMessage.setToUserName(map.get("FromUserName"));
        switch (replyMessage.getMsgType()){
            case WechatMessageUtil.MESSAGE_TEXT:
                String msgcont = map.get("Content").replace(" ","");
                System.out.println("文本消息=====>>"+msgcont);
                replyMessage.setContent(msgcont);
                break;
            case WechatMessageUtil.MESSAtGE_IMAGE:
                String picurl = map.get("PicUrl");
                System.out.println(picurl);
                replyMessage.setPicUrl(picurl);
                break;
            case WechatMessageUtil.MESSAGE_VOICE:
                System.out.println("vocie");
                break;
            case WechatMessageUtil.MESSAGE_LOCATION:
                System.out.println("坐标");
                break;
            case WechatMessageUtil.MESSAGE_EVENT_SUBSCRIBE:
                System.out.println("订阅");
                break;
            case WechatMessageUtil.MESSAGE_EVENT_UNSUBSCRIBE:
                System.out.println("取消");
                break;
            case WechatMessageUtil.MESSAGE_LINK:
                System.out.println("link");
                break;
        }
        return replyMessage;
    }

    // 封装微信公众号验证
    public static String getTestToken(HttpServletRequest request){
        // String signature, String timestamp,String nonce,String echostr
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        // 1.将token、timestamp、nonce三个参数进行字典序排序
        String[] arr = new String[] { token, timestamp, nonce };
        Arrays.sort(arr);

        // 2. 将三个参数字符串拼接成一个字符串进行sha1加密
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            // 将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        content = null;
        // 3.将sha1加密后的字符串可与signature对比，标识该请求来源于微信
        if (tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false){
            System.out.println(echostr);
            return echostr;
        }else {
            System.out.println("失败了");
            return "";
        }
    }

    // 查看用户信息是否已在库，不在则添加
    public static ReplyMessage getUserMessage(ReplyMessage replyMessage){
        String user_uid = replyMessage.getToUserName();
        String user_name = user_uid.substring(0,4);


        // 查找是否已经有账号，没有返回接口需求登录，有直接查询
        return replyMessage;
    }


}
